package com.ext.aic.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.SearchAdapter;
import com.ext.aic.apis.content.ProductListRequest;
import com.ext.aic.apis.gc.CompanyListRequest;
import com.ext.aic.beans.CompanyItem;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.beans.params.ProductListParams;
import com.ext.aic.databinding.SearchLayoutBinding;
import com.ext.aic.enums.SearchSource;
import com.ext.aic.handler.PageHandler;
import com.ext.aic.utils.ToastyUtils;

import java.util.Arrays;
import java.util.List;

public class SearchActivity extends BaseActivity {

    private SearchLayoutBinding binding;
    private SearchAdapter adapter;
    private String keyword;
    private SearchSource source;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.search_layout, null);
        binding = SearchLayoutBinding.bind(view);
        setContentView(binding.getRoot());
        bindView();
        bindData();
    }

    //region 初始化视图与数据绑定
    private void bindView() {
        binding.topView.backIv.setOnClickListener(v -> finish());
        binding.topView.searchTv.setOnClickListener(this::onSearchClick);
    }

    private void bindData() {
        source = SearchSource.getSearchSource(getBundleString("source"));

        if (source == SearchSource.CUSTOMERS) {
            binding.topView.searchEt.setHint(R.string.search_customers_hint);
        } else if (source == SearchSource.CONTENT) {
            binding.topView.searchEt.setHint(R.string.search_content_hint);
        }

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.content_list_divider));
        binding.searchListRv.addItemDecoration(divider);
        binding.searchListRv.setLayoutManager(new LinearLayoutManager(this));
        if (source == SearchSource.CONTENT) {
            adapter = new SearchAdapter<ProductItem>(this);
            adapter.setItems(productPageHandler.getItems());
        } else if (source == SearchSource.CUSTOMERS) {
            adapter = new SearchAdapter<CompanyItem>(this);
            adapter.setItems(companyPageHandler.getItems());
        }
        if (adapter != null) {
            binding.searchListRv.setAdapter(adapter);
        }
    }
    //endregion

    //region 内容数据请求
    private PageHandler<ProductItem> productPageHandler = new PageHandler<ProductItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            ProductListParams params = new ProductListParams();
            params.setPage(page);
            params.setPageSize(size);
            params.setContentStatus(Arrays.asList("self_created_approved", "others_created_approved"));
            params.setTitle(keyword);
            productListRequest.request(SearchActivity.this, SearchActivity.this, params, true);
        }
    };

    private ProductListRequest productListRequest = new ProductListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onProductListSuccess(List<ProductItem> data, Pagination pagination) {
            productPageHandler.loadDone(binding.refreshListSrl, pagination, data);
            adapter.notifyDataSetChanged();
        }
    };
    //endregion

    //region 企业数据请求
    private PageHandler<CompanyItem> companyPageHandler = new PageHandler<CompanyItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            companyListRequest.request(SearchActivity.this, SearchActivity.this, page, size, "", keyword);
        }
    };

    private CompanyListRequest companyListRequest = new CompanyListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        protected void onCompanyListSuccess(List<CompanyItem> companyItems, Pagination pagination) {
            companyPageHandler.loadDone(binding.refreshListSrl, pagination, companyItems);
            adapter.notifyDataSetChanged();
        }
    };
    //endregion

    public void onSearchClick(View view) {
        keyword = binding.topView.searchEt.getText().toString();
        if (TextUtils.isEmpty(keyword)) {
            String searchHint = getString(source == SearchSource.CUSTOMERS ? R.string.search_customers_hint : R.string.search_content_hint);
            ToastyUtils.getInstance().show(searchHint);
            return;
        }
        if (source == SearchSource.CUSTOMERS) {
            companyPageHandler.refresh();
        } else {
            productPageHandler.refresh();
        }
    }
}
